<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/11/30
 * Time: 14:18
 */

namespace app\sports_admin\service;
use app\sports_admin\model\Customer as CustomerModel;
use think\Request;
use app\exception\CustomerException as CustomerEx;


class Customer
{
    /**
     * 添加代理人信息
     */
    public static function customerAdd()
    {
        $message = Request::instance()->only(['name','pwd','address','phone','type','email','remark']);
        //判断用户是否已经注册过
        $is_register = CustomerModel::where("phone",$message['phone'])->find();
        if($is_register){
            throw new CustomerEx([
                'msg'=>'账号已经存在',
                'errorCode'=>400
            ]);
        }
        //添加用户信息
        $data    = CustomerModel::create([
            'name'=>trim($message['name']),
            'address'=>trim($message['address']),
            'phone'=>trim($message['phone']),
            'type'=>trim($message['type']),
            'email'=>trim($message['email']),
            'remark'=>trim($message['remark']),
            'pwd'=>authcode(trim($message['pwd']),'ENCODE',trim($message['phone']),0)
        ]);
        //判断是否添加成功
        if(!$data){
            throw new CustomerEx([
                'msg'=>'代理信息添加失败',
                'errorCode'=>400
            ]);
        }
        return true;
    }

    public static function showList()
    {
        return CustomerModel::showList();
    }

    public static function updateCustomer($id,$name,$phone,$tel,$remark,$email,$type)
    {
        return $result = CustomerModel::where('id',$id)->Update("
            name    = $name,
            phone   = $phone,
            tel     = $tel,
            remark  = $remark,
            email   = $email,
            type    = $type
        ");
    }

}